USE [xRepair]
GO

/****** Object:  StoredProcedure [dbo].[AddPendingOrderByUsername]    Script Date: 3/20/2014 11:39:32 PM ******/
DROP PROCEDURE [dbo].[AddPendingOrderByUsername]
GO

/****** Object:  StoredProcedure [dbo].[AddPendingOrderByUsername]    Script Date: 3/20/2014 11:39:32 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[AddPendingOrderByUsername]
	@Username			NVARCHAR(50),
	@Address			NVARCHAR(MAX),
	@Device				NVARCHAR(MAX),
	@SymptomInfo		NVARCHAR(MAX),
	@NewIdentity		INT = NULL OUTPUT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    
	IF NOT EXISTS(
					SELECT * 
					FROM [dbo].[PendingOrders] WITH (NOLOCK) 
					WHERE [Username] = @Username)
		BEGIN

			INSERT INTO [dbo].[PendingOrders]
				([Username]
				,[Address]
				,[Device]
				,[SymptomInfo])
			VALUES
				(@Username,
				 @Address,
				 @Device,
				 @SymptomInfo)

			SELECT @NewIdentity = @@IDENTITY

		END
	ELSE
		BEGIN
			UPDATE [dbo].[PendingOrders]
				SET 
				 [Address] = @Address
				,[Device] = @Device
				,[SymptomInfo] = @SymptomInfo
				,@NewIdentity = [ID]
			WHERE [Username] = @Username
		END
END

GO


